import store from '@/store/index.js'
import router from './index.js'

// 白名单
const whiteList = ['/login']

router.beforeEach(async(to, from, next) => {
    // to and from are both route objects. must call `next`.
    if(store.getters.token) {
        if (to.path === '/login') {
            next('/')
        } else {
            if(!store.getters.isUserInfo) {
                await store.dispatch('user/getUserInfo')
            }
            next()
        }
    }else {
        if (whiteList.indexOf(to.path) > -1) {
            next()
        } else {
            next('/login')
        }
    }
})